import axios from 'axios';
import * as constants from './constants';
import { fromJS } from 'immutable';

const changHomeData = (result) => ({
	type: constants.CHANGE_HOME_DATA,
	articleList: result,
});

const addHomeList = (list, nextPage) => ({
	type: constants.ADD_ARTICLE_LIST,
	list: fromJS(list),
	nextPage
});

const getProject = (data) => ({
	type: constants.GET_PROJECT_LIST,
	data: fromJS(data)
})

export const getHomeInfo = () => {
	return (dispatch) => {
		axios.get('/api/article/list', {
			params: {
				page: 1,
				pageSize: 10
			}
		}).then((res) => {
			const result = res.data.data;
			dispatch(changHomeData(result));
		});
	}
}

export const getMoreList = (page) => {
	return (dispatch) => {
		axios.get('/api/article/list?page=' + page + "&pageSize=10").then((res) => {
			const result = res.data.data;
			dispatch(addHomeList(result, page + 1));
		});
	}
}

export const getProjectList = () => (dispatch)  => {
	axios.get('/api/project/list').then((res)=> {
		dispatch(getProject(res.data.data));
	})
}

export const toggleTopShow = (show) => ({
	type: constants.TOGGLE_SCROLL_TOP,
	show
})